package com.wp.编程之法.chapter1;

/**
 * Created by 王萍 on 2017/4/2 0002.
 */
public class 字符串旋转 {
    private static char[] rotate(char[] chars, int start, int end) {
        while (start < end) {
            char tmp = chars[start];
            chars[start++] = chars[end];
            chars[end--] = tmp;
        }
        return chars;
    }

    public static char[] rotateSentence(char[] sentence) {
        sentence = rotate(sentence, 0, sentence.length - 1);
        int start = 0;
        int end = 0;
        int i = 0;
        while (i < sentence.length) {
            while (sentence[i] != ' '&&i < sentence.length-1) {
                i++;
            }
            end = i - 1;
            sentence = rotate(sentence, start, end);
            start = ++i;
        }
        return sentence;
    }

    public static void main(String[] args) {
        String str = "I am a student.";
        char[] rotateSentence = rotateSentence(str.toCharArray());
        System.out.println(new String(rotateSentence));
    }
}
